const app = getApp();
const http = require('../../../utils/http.js')
Page({
    /**
     * 页面的初始数据
     */
    data: {
        pageNum1: 1,
        pageNum2: 1,
        pageNum3: 1,
        pageSize: 15,
        TabCur: 0,
        SortMenu: [{id: 0, name: "待付款"}, {id: 1, name: "已付款"}, {id: 2, name: "已退款"}],
        orderList: [],
        isShow: false,
        outTradeNo:"",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            customer: app.globalData.customer
          })
        let that = this;
        let postData = {status: this.data.TabCur,pageNum: 1,pageSize: this.data.pageSize}
        this.selectMyOrderLit(postData)
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    selectMyOrderLit(postData) {
        
        http.get('/api/user/myOrder', postData).then(res => {
            if (res.code == 200) {
                this.setData({
                    orderList: res.data.list,
                    hasNextPage: res.data.hasNextPage,
                })
                if(this.data.TabCur==0){
                    this.setData({
                      pageNum1:2
                    })
                }
                if(this.data.TabCur==1){
                    this.setData({
                      pageNum2:2
                    })
                }
                if(this.data.TabCur==2){
                    this.setData({
                      pageNum3:2
                    })
                }
            }
        })
    },

    tabSelect(e) {
        this.setData({
            TabCur: e.currentTarget.dataset.id
        })
        let postData = {status: e.currentTarget.dataset.id,pageNum: 1,pageSize: this.data.pageSize}
        this.selectMyOrderLit(postData)
    },

    orderClick(e) {
        let that = this;
        let index = e.currentTarget.dataset.index;
        let order = this.data.orderList[index];
        if (order.status == 0) {
            wx.showLoading({
                title: '加载中。。。',
                mask: true
            })
            http.get('/api/purchase/payTradeStatus0', {prepayId: order.prepayId}).then(res => {
                wx.hideLoading()
                if (res.code == 200) {
                    wx.requestPayment({
                        timeStamp: res.data.timeStamp,
                        nonceStr: res.data.nonceStr,
                        package: res.data.package,
                        signType: res.data.signType,
                        paySign: res.data.paySign,
                        success(res) {
                            if (res.errMsg = "requestPayment:ok") {
                                let postData = {status: that.data.TabCur,pageNum: 1,pageSize: that.data.pageSize}
                                that.selectMyOrderLit(postData)
                            }
                        }
                    })
                }
            })
        }
        if (order.status == 1) {
            console.log(e.currentTarget.dataset.id)
            if(e.currentTarget.dataset.type==1){
                wx.navigateTo({
                    url: '../orderDetail/detail?id='+e.currentTarget.dataset.id,
                })
            }
        }
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        console.log("上拉分页")
        var TabCur = this.data.TabCur
        var url, listTemp, pageNum, list
        list=this.data.orderList
        const that = this
        wx.showLoading({
            title: '加载中。。。',
            mask: true
        })
        if (TabCur == 0) {
            pageNum = that.data.pageNum1;
        } 
        if (TabCur == 1) {
            pageNum = that.data.pageNum2;
        } 
        if (TabCur == 2) {
            pageNum = that.data.pageNum3;
        }
        if (TabCur == 3) {
            pageNum = that.data.pageNum4;
        }
        if (this.data.hasNextPage) {
            http.get('/api/user/myOrder', {status: this.data.TabCur,pageNum: pageNum, pageSize: this.data.pageSize}).then(res => {
                wx.hideLoading()
                if (res.code == 200) {
                    if (TabCur == 0) {
                        this.setData({
                            pageNum1: res.data.pageNum + 1,
                            hasNextPage: res.data.hasNextPage,
                            orderList: list.concat(res.data.list)
                        })
                    }
                    if (TabCur == 1) {
                        this.setData({
                            pageNum2: res.data.pageNum + 1,
                            hasNextPage: res.data.hasNextPage,
                            orderList: list.concat(res.data.list)
                        })
                    }
                    if (TabCur == 2) {
                        this.setData({
                            pageNum3: res.data.pageNum + 1,
                            hasNextPage: res.data.hasNextPage,
                            orderList: list.concat(res.data.list)
                        })
                    }
                }
            })
        } else {
            wx.showToast({
                title: '没有更多数据',
                icon: "none"
            })
        }
    }
})

